import { defineStore } from 'pinia';
import { store } from '@/store';

interface PlcState {
  isOnLine: boolean;
  notOnlineNameList: any;
  newData: any;
}

export const usePlcStore = defineStore({
  id: 'app-plc-data',
  state: (): PlcState => ({
    isOnLine: false,
    notOnlineNameList: [],
    newData: {}
  }),
  getters: {
    gePlcIsOnLine(): boolean {
      return this.isOnLine;
    },
  },
  actions: {
    updateNewData(data = {}) {
      if (data) {
        this.newData = {
          ...this.newData,
          ...data
        };
      }
    },
    setIsOnLine(isOnLine) {
      this.isOnLine = isOnLine;
    },
    setNotOnlineNameFn(name) {
      if (!this.notOnlineNameList.includes(name)) {
        this.notOnlineNameList.push(name);
      }
    },
    removeNotOnlineNameFn(name) {
      if (this.notOnlineNameList.includes(name)) {
        this.notOnlineNameList.splice(this.notOnlineNameList.indexOf(name), 1);
      }
    }
  }
});

// Need to be used outside the setup
export function usePlcWithOut() {
  return usePlcStore(store);
}
